package com.example.xiaozu.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupMenu;
import android.widget.TextView;
import com.example.xiaozu.R;
import com.example.xiaozu.model.User;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class AccountAdapter extends RecyclerView.Adapter<AccountAdapter.AccountViewHolder> {

    private final Context context;
    private final List<User> accountList;
    private final AccountActionListener listener;

    public interface AccountActionListener {
        void onEditAccount(User user);
        void onDeleteAccount(int position, User user);
        void onToggleAdminStatus(int position, User user);
    }

    public AccountAdapter(Context context, List<User> accountList, AccountActionListener listener) {
        this.context = context;
        this.accountList = accountList;
        this.listener = listener;
    }

    @NonNull
    @Override
    public AccountViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context)
                .inflate(R.layout.item_account, parent, false);
        return new AccountViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AccountViewHolder holder, int position) {
        User user = accountList.get(position);
        holder.bind(user);

        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onEditAccount(user);
            }
        });

        holder.itemView.setOnLongClickListener(v -> {
            showPopupMenu(v, position, user);
            return true;
        });
    }

    private void showPopupMenu(View anchorView, int position, User user) {
        PopupMenu popupMenu = new PopupMenu(context, anchorView);
        popupMenu.inflate(R.menu.menu_account_actions);

        popupMenu.getMenu().findItem(R.id.menu_set_admin).setVisible(!user.isAdmin());
        popupMenu.getMenu().findItem(R.id.menu_remove_admin).setVisible(user.isAdmin());

        popupMenu.setOnMenuItemClickListener(item -> {
            int itemId = item.getItemId();
            if (itemId == R.id.menu_set_admin || itemId == R.id.menu_remove_admin) {
                listener.onToggleAdminStatus(position, user);
                return true;
            } else if (itemId == R.id.menu_delete) {
                listener.onDeleteAccount(position, user);
                return true;
            }
            return false;
        });
        popupMenu.show();
    }

    @Override
    public int getItemCount() {
        return accountList.size();
    }

    public void updateAccountList(List<User> newList) {
        accountList.clear();
        accountList.addAll(newList);
        notifyDataSetChanged();
    }

    static class AccountViewHolder extends RecyclerView.ViewHolder {
        private final TextView tvUsername;
        private final TextView tvPhone;
        private final TextView tvAdminFlag;

        public AccountViewHolder(@NonNull View itemView) {
            super(itemView);
            tvUsername = itemView.findViewById(R.id.tv_username);
            tvPhone = itemView.findViewById(R.id.tv_phone);
            tvAdminFlag = itemView.findViewById(R.id.tv_admin_flag);
        }

        public void bind(User user) {
            tvUsername.setText(user.getUsername());
            tvPhone.setText(user.getPhone());
            tvAdminFlag.setVisibility(user.isAdmin() ? View.VISIBLE : View.GONE);
        }
    }
}
